//
//  RW_UserSingleListItemCell.m
//  SleepApp
//
//  Created by chuangqi on 2021/4/7.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_UserSingleListItemCell.h"
#import "RW_RemoveView.h"
#import "RW_UserSongModel.h"
@interface RW_UserSingleListItemCell (){
    RW_UserSingleListItemCellModel *_model;
}
/** 歌曲图片 */
@property (nonatomic,strong) UIImageView *wlclass_image_view;
/** 标题 */
@property (nonatomic,strong) UILabel *wlclass_title_label;
/** 副标题*/
@property (nonatomic,strong) UILabel *wlclass_desc_label;

/** 删除 */
@property (nonatomic,strong) UIButton *wlclass_delete_button;
@end
@implementation RW_UserSingleListItemCell

-(void)bind_row_data:(WLBaseCollectionViewLayerRow *)sender{
    if ([sender.data isKindOfClass:RW_UserSingleListItemCellModel.class]){
        _model = sender.data;
        
        self.wlclass_title_label.text = _model.title_string;
        self.wlclass_desc_label.text = _model.desc_string;
        
        [self.wlclass_image_view wl_setImage:_model.cover_url];
        
        if (_model.isEdit){
            [self.wlclass_desc_label mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(self).mas_offset(-8);
                make.left.mas_equalTo(self.wlclass_image_view.mas_right).mas_offset(10);
                make.right.mas_equalTo(self.wlclass_delete_button.mas_left).mas_offset(-10);
            }];
        } else {
            [self.wlclass_desc_label mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(self).mas_offset(-8);
                make.left.mas_equalTo(self.wlclass_image_view.mas_right).mas_offset(10);
                make.right.mas_equalTo(self).mas_offset(-10);
            }];
        }
//        [self layoutIfNeeded];
        
        
        self.wlclass_delete_button.hidden = !_model.isEdit;
    }
}
-(void)bind_delegate:(id)sender{
    self.delegate = sender;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        [self addSubview:self.wlclass_title_label];
        [self addSubview:self.wlclass_desc_label];
        [self addSubview:self.wlclass_image_view];
        
        [self addSubview:self.wlclass_delete_button];
        
        [self.wlclass_title_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).mas_offset(8);
            make.left.mas_equalTo(self.wlclass_image_view.mas_right).mas_offset(10);
        }];
        [self.wlclass_desc_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self).mas_offset(-8);
            make.left.mas_equalTo(self.wlclass_image_view.mas_right).mas_offset(10);
            make.right.mas_equalTo(self).mas_offset(-10);
        }];
        [self.wlclass_image_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).mas_offset(5);
            make.top.mas_equalTo(self).mas_offset(5);
            make.bottom.mas_equalTo(self).mas_offset(-5);
            make.width.mas_equalTo(self.wlclass_image_view.mas_height);
        }];
        [self.wlclass_delete_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self);
            make.right.mas_equalTo(self).mas_offset(-15);
            make.width.mas_offset(40);
        }];
    }
    return self;
}
#pragma mark -- 操作
-(void)click_delete_button:(UIButton *)button{
    __WEAK_SELF_YLSLIDE
    RW_RemoveView.init_wl()
    .maskAlpha_wl(.5f)
    .addData_wl(_model)
    .closeBlock_wl(^(RW_UserSingleListItemCellModel *data){
        [[[RW_UserSongModel deleteToUserSingleSongId:data.user_song_model.userSongId] then:^id _Nullable(NSString * _Nullable value) {
            if ([weakSelf.delegate respondsToSelector:@selector(reloadList)]){
                [weakSelf.delegate reloadList];
            }
            return nil;
        }] catch:^(NSError * _Nonnull error) {
            [SVProgressHUD showError_wl:error.domain];
        }] ;
    }).show_wl();
}

#pragma mark -- 懒加载
-(UILabel *)wlclass_title_label{
    if (!_wlclass_title_label){
        _wlclass_title_label = [[UILabel alloc]init];
        _wlclass_title_label.textColor = HexColor(@"333333");
        _wlclass_title_label.font = [UIFont systemFontOfSize:17 weight:UIFontWeightMedium];
    }
    return _wlclass_title_label;
}
-(UILabel *)wlclass_desc_label{
    if (!_wlclass_desc_label){
        _wlclass_desc_label = [[UILabel alloc]init];
        _wlclass_desc_label.textColor = HexColor(@"888888");
        _wlclass_desc_label.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    }
    return _wlclass_desc_label;
}
-(UIImageView *)wlclass_image_view{
    if (!_wlclass_image_view){
        _wlclass_image_view = [[UIImageView alloc]init];
        _wlclass_image_view.clipsToBounds = YES;
        _wlclass_image_view.contentMode = UIViewContentModeScaleAspectFill;
        _wlclass_image_view.layer.cornerRadius = 16;
    }
    return _wlclass_image_view;
}
-(UIButton *)wlclass_delete_button{
    if (!_wlclass_delete_button){
        _wlclass_delete_button = [[UIButton alloc]init];
        _wlclass_delete_button.hidden = true;
        [_wlclass_delete_button setImage:[UIImage svgName:@"user_song_delete"] forState:UIControlStateNormal];
        [_wlclass_delete_button addTarget:self action:@selector(click_delete_button:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _wlclass_delete_button;
}
@end

@implementation RW_UserSingleListItemCellModel
-(NSString *)title_string{
    if (!_title_string){
        _title_string = self.user_song_model.name;
    }
    return _title_string;
}
-(NSString *)desc_string{
    if (!_desc_string){
        _desc_string = self.user_song_model.desc;
    }
    return _desc_string;
}
-(NSString *)cover_url{
    if (!_cover_url){
        _cover_url = self.user_song_model.cover;
    }
    return _cover_url;
}
@end
